// --------------------------------------------------------------------------------------------------------------------- 
// <copyright file="T4Template.cs" company="Microsoft.Blueprints.Contrib Team">
//   (C) 2009 Microsoft.Blueprints.Contrib Team
// </copyright>
// <summary>
//   Defines the Template type.
// </summary>
// ---------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Blueprints.Contrib.T4
{
    #region Using Directives

    using System.Collections.Generic;
    using System.IO;
    using System.Text.RegularExpressions;

    using Microsoft.SoftwareFactories.Blueprints;

    #endregion

    /// <summary>
    /// Represents a T4 Template
    /// </summary>
    public class T4Template
    {
        /// <summary>
        /// Dictionary which contains the properties and data exposed by the T4 Template
        /// </summary>
        private Dictionary<string, T4PropertyData> properties = new Dictionary<string, T4PropertyData>();

        /// <summary>
        /// Initializes a new instance of the <see cref="T4Template"/> class.
        /// </summary>
        public T4Template()
        {
            this.Content = string.Empty;
            this.FileName = string.Empty;
            this.RenderedContent = string.Empty;
            this.TargetFileName = string.Empty;
        }

        /// <summary>
        /// Gets or sets the filename of the T4 template.
        /// </summary>
        public string Content
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the filepath of the T4 template within the content directory.
        /// </summary>
        public string ContentFilePath
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the filename of the T4 template.
        /// </summary>
        public string FileName
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the Properties.
        /// </summary>
        /// <value>
        /// The properties.
        /// </value>
        public Dictionary<string, T4PropertyData> Properties
        {
            get
            {
                return this.properties;
            }

            set
            {
                this.properties = value;
            }
        }

        /// <summary>
        /// Gets or sets RenderedContent.
        /// </summary>
        /// <value>
        /// The rendered content.
        /// </value>
        public string RenderedContent
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the filename of  transformed T4 template.
        /// </summary>
        public string TargetFileName
        {
            get;
            set;
        }

        public void Populate()
        {
            if (File.Exists(this.ContentFilePath))
            {
                this.Content = File.ReadAllText(this.ContentFilePath);
            }
        }

        /// <summary>
        /// Dynamically parses the T4 templage for the properties directives
        /// </summary>
        public void PopulateProperties()
        {
            if (this.Content.Length == 0)
            {
                this.Populate();
            }

            // Extract the value of the name attribute from the T4 property directive.
            const string Expression = "<#@.*processor=\"T4PropertyProcessor\".*name=\"(?<name>.*?)\".*#>";

            if (this.Content.Length > 0)
            {
                Regex regex = new Regex(Expression);

                MatchCollection matches = regex.Matches(this.Content);

                foreach (Match match in matches)
                {
                    this.Properties.Add(match.Groups["name"].Value.Trim(), new T4PropertyData(string.Empty, typeof(string)));
                }
            }
        }
    }
}